﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQ.InterviewQuestions.LinkedList
{
    internal class CircularLinkedList
    {
        public Node GenerateCircularLinkedList()
        {
            Node head = new Node(0);
            head.Next = new Node(1);
            head.Next.Next = new Node(2);
            head.Next.Next.Next = new Node(3);
            head.Next.Next.Next.Next = new Node(4);
            head.Next.Next.Next.Next.Next = head.Next.Next;
            return head;
        }

        public bool IsCircular(Node h)
        {
            Node fast, slow;
            fast = slow = h;

            while (true)
            {
                if (fast == null || fast.Next == null)
                {
                    return false;
                }

                slow = slow.Next;
                fast = fast.Next.Next;
                
                if (fast == slow)
                {
                    return true;
                }
            }

        }
    }
}
